unit functions;

interface

uses Windows, SysUtils, Bass, dll, Controls, ShlObj, Forms, Menus, header, Graphics;

function StartBass(H: HWND; var BASS_Float: Boolean): Boolean;
function LabelRun(inp: WideString): WideString;
function Loop(inp, max: integer): integer;
function TotalSeconds(h: Cardinal): Word;
function FormatTime(h: Cardinal): string;
function BrowseDialog(const Title: string; const Flag: integer): string;
function GetSize(Channel: Cardinal): Cardinal;
function GetBitRate(Channel: Cardinal): Cardinal;
function GetRevisionStr: string;
function GetBitmap(Name: PAnsiChar): HBITMAP;
function LoadFont(FontFile: PAnsiChar): Boolean;
function UnLoadFont(FontFile: PAnsiChar): Boolean;
function IsFontLoaded(FontName: TFontName): Boolean;

implementation

function StartBass(H: HWND; var BASS_Float: Boolean): Boolean;
begin
  Result := True;

  if HiWord(BASS_GetVersion) <> BASSVERSION then
    begin
      MessageBox(H, 'Wrong sound engine version!', 'Error', MB_ICONERROR or MB_OK);
      Result := False;
      Exit;
    end;

  if not BASS_Init(1, 44100, 0, H, nil) then
    begin
      MessageBox(H, 'Couldn''t initialize device!', 'Warning', MB_ICONWARNING or MB_OK);
      BASS_Init(0, 44100, 0, H, nil); //No sound
    end;

  // Can float samples be used?
  if BASS_Float then
    if BASS_StreamCreate(44100, 2, BASS_SAMPLE_FLOAT, nil, 0) > 0 then
      BASS_Float := True
    else
      BASS_Float := False;
end;

function LabelRun(inp: WideString): WideString;
var
  temp: WideString;
begin
  temp := copy(inp, 1, 1);
  Delete(inp, 1, 1);
  Result := inp + temp;
end;

{$R-}
function Loop(inp, max: integer): integer;
var
  i: integer;
begin
  repeat
    i := Random(max);
  until
    i <> inp;

  Result := i;
end;

function TotalSeconds(h: Cardinal): Word;
begin
  Result := Round(BASS_ChannelBytes2Seconds(h, BASS_ChannelGetLength(h)));
end;

function FormatTime(h: Cardinal): string;
var
  Total1, Total2: Word;
begin
  Total1 := Round(BASS_ChannelBytes2Seconds(h, BASS_ChannelGetPosition(h)));
  Total2 := Round(BASS_ChannelBytes2Seconds(h, BASS_ChannelGetLength(h)));
  Result := PChar(SecToTime(Total1)) + '/' + PChar(SecToTime(Total2));
end;
{$R+}

function BrowseDialogCallBack(Wnd: HWND; uMsg: UINT; lParam, lpData: LPARAM): integer stdcall;
var
  wa,
  rect : TRect;
  dialogPT : TPoint;
begin
  if uMsg = BFFM_INITIALIZED then
    begin
      wa := Screen.WorkAreaRect;
      GetWindowRect(Wnd, Rect);
      dialogPT.X := ((wa.Right-wa.Left) div 2) - ((rect.Right-rect.Left) div 2);
      dialogPT.Y := ((wa.Bottom-wa.Top) div 2) - ((rect.Bottom-rect.Top) div 2);
      MoveWindow(Wnd, dialogPT.X, dialogPT.Y, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top, True);
    end;

  Result := 0;
end;

function BrowseDialog(const Title: string; const Flag: integer): string;
var
  lpItemID: PItemIDList;
  BrowseInfo: TBrowseInfo;
  DisplayName: array[0..MAX_PATH] of char;
  TempPath: array[0..MAX_PATH] of char;
begin
  Result := '';
  FillChar(BrowseInfo, sizeof(TBrowseInfo), #0);

  with BrowseInfo do
    begin
      hwndOwner := Application.Handle;
      pszDisplayName := @DisplayName;
      lpszTitle := PChar(Title);
      ulFlags := Flag;
      lpfn := BrowseDialogCallBack;
    end;

  lpItemID := SHBrowseForFolder(BrowseInfo);

  if lpItemId <> nil then
    begin
      SHGetPathFromIDList(lpItemID, TempPath);
      Result := TempPath;
      GlobalFreePtr(lpItemID);
    end;
end;

function GetSize(Channel: Cardinal): Cardinal;
begin
  Result := BASS_StreamGetFilePosition(Channel, BASS_FILEPOS_END);
end;

function GetBitRate(Channel: Cardinal): Cardinal;
var
  FileLength: Cardinal;
  SongLength: Int64;
  FloatPos: FLOAT;
begin
  FileLength := BASS_StreamGetFilePosition(Channel, BASS_FILEPOS_END);
  SongLength := BASS_ChannelGetLength(Channel);
  FloatPos   := BASS_ChannelBytes2Seconds(Channel, SongLength);
  Result     := Trunc((FileLength / (125 * FloatPos)) + 0.5);

  if Odd(Result) then
    Dec(Result);
end;

function GetRevisionStr: string;
var
  buffer : array[0..255] of char;
begin
  Result := '';

  if LoadString(HInstance, 1001, buffer, sizeof(buffer)) <> 0 then
    Result := buffer;
end;

function GetBitmap(Name: PAnsiChar): HBITMAP;
begin
  Result := LoadBitmap(HInstance, Name);
end;

function LoadFont(FontFile: PAnsiChar): Boolean;
begin
  Result := (AddFontResourceEx(FontFile, FR_PRIVATE, NIL) = 1);
end;

function UnLoadFont(FontFile: PAnsiChar): Boolean;
begin
  Result := RemoveFontResourceEx(FontFile, FR_PRIVATE, NIL);
end;

function IsFontLoaded(FontName: TFontName): Boolean;
begin
  Result := (Screen.Fonts.IndexOf(FontName) >= 0);
end;


end.
